<?php

include_once($_SERVER['DOCUMENT_ROOT'] . '/htqlmba/configs/database.php');
class DonVi extends database {
    
    private $MA_DON_VI;
    private $TEN_DON_VI;
    private $DIA_CHI_DV;
    private $LIEN_HE_DV;
    private $TEN_TK;
    private $MAT_KHAU;
    private $QUYEN;

    public function setMaDV($MADV) {
        $this->MA_DON_VI = $MADV;
    }

    public function getMaDV() {
        return $this->MA_DON_VI;
    }

    public function setTenDV($TENDV) {
        $this->TEN_DON_VI = $TENDV;
    }

    public function getTenDV() {
        return $this->TEN_DON_VI;
    }

    public function setDiaChiDV($DiaChiDV) {
        $this->DIA_CHI_DV = $DiaChiDV;
    }

    public function getDiaChiDV() {
        return $this->DIA_CHI_DV;
    }

    public function setLienHeDV($LienHeDV) {
        $this->LIEN_HE_DV = $LienHeDV;
    }

    public function getLienHeDV() {
        return $this->LIEN_HE_DV;
    }

    public function setTenTK($TaiKhoan) {
        $this->TEN_TK = $TaiKhoan;
    }

    public function getTenTK() {
        return $this->TEN_TK;
    }

    public function setMatKhau($MatKhau) {
        $this->MAT_KHAU = $MatKhau;
    }

    public function getMatKhau() {
        return $this->MAT_KHAU;
    }

    public function setQuyen($Quyen) {
        $this->QUYEN = $Quyen;
    }

    public function getQuyen() {
        return $this->QUYEN;
    }

    // function them thong tin don vi
    public function themDV() {
        $query = "insert into don_vi(MA_DON_VI,TEN_DON_VI,DIA_CHI_DV,LIEN_HE_DV) 
            values ('" . $this->getMaDV() . "','" . $this->getTenDV() . "','" . $this->getDiaChiDV() . "','" . $this->getLienHeDV() . "')";
        $this->setQuery($query);
        $this->executeQuery();
        $query = "insert into tai_khoan(TEN_TK,MAT_KHAU,MA_DON_VI,QUYEN) 
            values ('" . $this->getTenTK() . "','" . $this->getMatKhau() . "','" . $this->getMaDV() . "','" . $this->getQuyen() . "')";
        $this->setQuery($query);
        return $this->executeQuery();
    }

    // function sua thong tin don vi
    public function suaDV() {
        $query = "		update don_vi 
						set TEN_DON_VI='" . $this->getTenDV() . "',
						DIA_CHI_DV='" . $this->getDiaChiDV() . "',
						LIEN_HE_DV='" . $this->getLienHeDV() . "'
						where MA_DON_VI='" . $this->getMaDV() . "' ";
        $this->setQuery($query);
        $this->executeQuery();

        $query = "	update tai_khoan 
						set TEN_TK='" . $this->getTenTK() . "',
						MAT_KHAU='" . $this->getMatKhau() . "',
						QUYEN='" . $this->getQuyen() . "'
						where MA_DON_VI='" . $this->getMaDV() . "' ";
        $this->setQuery($query);
        return $this->executeQuery();
    }

    // function xoa thong tin don vi
    public function xoaDV() {
		// them nhung dong tin chi_tiet_so_huu cua don_vi bi xoa vao chi_tiet_so_huu_backup
			$db = new database();
			$db->setQuery("select SO_N0,MA_DON_VI,NGAY_CHUYEN_DEN FROM chi_tiet_so_huu WHERE MA_DON_VI='" . $this->getMaDV() . "'");
			$result = $db->fetchAll();
			 $num = mysql_num_rows($result);
			 if ($num>0){
			 while($row = mysql_fetch_array($result)){	
			 // them thong tin bi xoa sang chi tiet so huu backup
			 $db6 = new database();
			  $query = "	insert into chi_tiet_so_huu_backup(SO_N0,MA_DON_VI,NGAY_CHUYEN_DEN)
						VALUES ('" .$row['SO_N0']. "','" .$row['MA_DON_VI']. "','" .$row['NGAY_CHUYEN_DEN']. "')
						";
            $db6->setQuery($query);
            $db6->executeQuery();
			
			// them nhung dong tin may_bien_ap cua don_vi bi xoa vao may_bien_ap_backup
			$db2 = new database();
			$db2->setQuery("select MSTS,SO_N0,MA_DIEN_AP,MA_NSX,MA_LOAI,TEN_LOAI_DAU,CONG_SUAT,CHIEU_DAI,CHIEU_RONG,CHIEU_CAO,RUOT_MAY,DAU_MAY,TONG,NAM_SX,NAM_NHAP_VE,THONG_SO_DO,NHAN_XET,GHI_CHU,SAP_XEP FROM may_bien_ap WHERE SO_N0='" .$row['SO_N0']. "'");
			$result2 = $db2->fetchAll();
			 $num2 = mysql_num_rows($result2);
			 if ($num2>0){
			 while($row2 = mysql_fetch_array($result2)){
				 // them nhung dong tin may_bien_ap cua don_vi bi xoa vao may_bien_ap_backup	
			 	  $query3 = "insert into may_bien_ap_backup(MSTS,SO_N0,MA_DIEN_AP,MA_NSX,MA_LOAI,TEN_LOAI_DAU,CONG_SUAT,CHIEU_DAI,CHIEU_RONG,CHIEU_CAO,RUOT_MAY,DAU_MAY,TONG,NAM_SX,NAM_NHAP_VE,THONG_SO_DO,NHAN_XET,GHI_CHU,SAP_XEP)
			values ('" .$row2['MSTS']. "','" .$row2['SO_N0']. "','" .$row2['MA_DIEN_AP']. "','" .$row2['MA_NSX']. "','" .$row2['MA_LOAI']. "','" .$row2['TEN_LOAI_DAU']. "','" .$row2['CONG_SUAT']. "','" .$row2['CHIEU_DAI']. "','" .$row2['CHIEU_RONG']. "','" .$row2['CHIEU_CAO']. "','" .$row2['RUOT_MAY']. "','" .$row2['DAU_MAY']. "','" .$row2['TONG']. "','" .$row2['NAM_SX']. "','" .$row2['NAM_NHAP_VE']. "','" .$row2['THONG_SO_DO']. "','" .$row2['NHAN_XET']. "','" .$row2['GHI_CHU']. "','" .$row2['SAP_XEP']. "')";
        $this->setQuery($query);
        $this->executeQuery();
		
				
				// XOA nhung dong tin may_bien_ap
				$db4 = new database();
				$db4->setQuery("delete from may_bien_ap where  SO_N0='" .$row['SO_N0']. "'");
				 $db4->executeQuery();
			 }
			 }
			 // xoa trong bang chi tiet so huu
			 $this->setQuery("delete from chi_tiet_so_huu where  SO_N0='" .$row['SO_N0']. "' AND
			 				  			 						 MA_DON_VI='" .$row['MA_DON_VI']. "' AND
																 NGAY_CHUYEN_DEN='" .$row['NGAY_CHUYEN_DEN']. "'
			 				");
       		$this->executeQuery();
			}
			 }
		
		// them don vi bi xoa bao backup
		 $query = "insert into don_vi_backup(MA_DON_VI,TEN_DON_VI,DIA_CHI_DV,LIEN_HE_DV) 
            values ('" . $this->getMaDV() . "','" . $this->getTenDV() . "','" . $this->getDiaChiDV() . "','" . $this->getLienHeDV() . "')";
        $this->setQuery($query);
        $this->executeQuery();
		
        $this->setQuery("delete from don_vi where  MA_DON_VI='" . $this->getMaDV() . "'");
        $this->executeQuery();
        $this->setQuery("delete from tai_khoan where  MA_DON_VI='" . $this->getMaDV() . "'");
        return $this->executeQuery();
    }

    public function dsDonVi() {
        $this->setQuery("select don_vi.MA_DON_VI,TEN_DON_VI,DIA_CHI_DV,LIEN_HE_DV,TEN_TK,MAT_KHAU,QUYEN
							 from don_vi,tai_khoan
							 WHERE don_vi.MA_DON_VI=tai_khoan.MA_DON_VI
ORDER BY `don_vi`.`MA_DON_VI`  DESC");
        return $this->fetchAll();
    }

}

?>